﻿@{
    Layout = "~/Views/Shared/_LayoutAdminPage.cshtml";
}
<!-- 正文开始 -->
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <!-- 表格工具栏 -->
            <form class="layui-form toolbar">
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">名称:</label>
                        <div class="layui-input-inline">
                            <input name="name" class="layui-input" placeholder="输入名称" />
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label">等&emsp;级:</label>
                        <div class="layui-input-inline">
                            <select name="level">
                                <option value="0">选择等级</option>
                                <option value="1">省</option>
                                <option value="2">市</option>
                                <option value="3">区</option>
                            </select>
                        </div>
                    </div>
                    <div class="layui-inline">
                        &emsp;
                        <button class="layui-btn icon-btn" lay-filter="regionTbSearch" lay-submit>
                            <i class="layui-icon">&#xe615;</i>搜索
                        </button>
                    </div>
                </div>
            </form>
            <!-- 数据表格 -->
            <table id="regionTable" lay-filter="regionTable"></table>
        </div>
    </div>
</div>


<!-- 表格操作列 -->
<script type="text/html" id="regionTbBar">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">修改</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>


<!-- 表单弹窗 -->
<script type="text/html" id="regionEditDialog">
    <form id="regionEditForm" lay-filter="regionEditForm" class="layui-form model-form">
        <div class="layui-form-item">
            <label class="layui-form-label layui-form-required">标识:</label>
            <div class="layui-input-block">
                <input name="id" placeholder="请输入标识" class="layui-input"
                       lay-verType="tips" lay-verify="required" required />
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label layui-form-required">区域名称:</label>
            <div class="layui-input-block">
                <input name="name" placeholder="请输入区域名称" class="layui-input"
                       lay-verType="tips" lay-verify="required" required />
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label layui-form-required">区域上级标识:</label>
            <div class="layui-input-block">
                <input name="pid" placeholder="请输入区域上级标识" class="layui-input" type="number"
                       lay-verType="tips" lay-verify="required" required />
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label layui-form-required">地名简称:</label>
            <div class="layui-input-block">
                <input name="sname" placeholder="请输入地名简称" class="layui-input"
                       lay-verType="tips" lay-verify="required" required />
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label layui-form-required">区域等级:</label>
            <div class="layui-input-block">
                <select name="level" lay-verType="tips" lay-verify="required">
                    <option value="">请选择区域等级</option>
                    <option value="1">省</option>
                    <option value="2">市</option>
                    <option value="3">区</option>
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label layui-form-required">区域编码:</label>
            <div class="layui-input-block">
                <input name="citycode" placeholder="请输入区域编码" class="layui-input"
                       lay-verType="tips" lay-verify="required" required />
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label layui-form-required">邮政编码:</label>
            <div class="layui-input-block">
                <input name="yzcode" placeholder="请输入邮政编码" class="layui-input"
                       lay-verType="tips" lay-verify="required" required />
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label layui-form-required">组合名称:</label>
            <div class="layui-input-block">
                <input name="mername" placeholder="请输入组合名称" class="layui-input"
                       lay-verType="tips" lay-verify="required" required />
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label layui-form-required">经度:</label>
            <div class="layui-input-block">
                <input name="Lng" placeholder="请输入经度" class="layui-input"
                       lay-verType="tips" lay-verify="required" required />
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label layui-form-required">纬度:</label>
            <div class="layui-input-block">
                <input name="Lat" placeholder="请输入纬度" class="layui-input"
                       lay-verType="tips" lay-verify="required" required />
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label layui-form-required">拼音:</label>
            <div class="layui-input-block">
                <input name="pinyin" placeholder="请输入拼音" class="layui-input"
                       lay-verType="tips" lay-verify="required" required />
            </div>
        </div>
        <div class="layui-form-item text-right">
            <button class="layui-btn" lay-filter="regionEditSubmit" lay-submit>保存</button>
            <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">取消</button>
        </div>
    </form>
</script>


<script>
    layui.use(['layer', 'form', 'table', 'util', 'admin', 'zTree','dropdown'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var util = layui.util;
        var admin = layui.admin;

        /* 渲染表格 */
        var insTb = table.render({
            elem: '#regionTable',
            url: '/api/admin/Region/GetRegionList',
            page: true,
            toolbar: ['<p>',
                '<button lay-event="add" class="layui-btn layui-btn-sm icon-btn"><i class="layui-icon">&#xe654;</i>添加</button>&nbsp;',
                '<button lay-event="del" class="layui-btn layui-btn-sm layui-btn-danger icon-btn"><i class="layui-icon">&#xe640;</i>删除</button>',
                '</p>'].join(''),
            cellMinWidth: 100,
            cols: [[
                { type: 'checkbox' },
                { field: 'id', width: 120, title: '标识' },
                {field: 'name', title: '区域名称', sort: true},
                { field: 'pid', title: '上级标识', sort: true},
                { field: 'sname', title: '地名简称', sort: true},
                {
                    field: 'level', title: '区域等级', templet: function (d) {
                        if (!d.level) {
                            return "";
                        }else if(d.level == "1") {
                            return "省";
                        } else if (d.level == "2") {
                            return "市";
                        } else if (d.level == "3") {
                            return "区";
                        }
                        //return util.toDateString(d.createTime);
                    }, sort: true
                },
                { field: 'citycode', title: '区域编码', sort: true },
                { field: 'yzcode', title: '邮政编码', sort: true },
                { field: 'mername', title: '组合名称', sort: true },
                { field: 'Lng', title: '经度', sort: true },
                { field: 'Lat', title: '纬度', sort: true },
                { field: 'pinyin', title: '拼音', sort: true },
                {title: '操作', toolbar: '#regionTbBar', align: 'center', width: 200, minWidth: 200}
            ]]
        });

        /* 表格搜索 */
        form.on('submit(regionTbSearch)', function (data) {
            insTb.reload({where: data.field, page: {curr: 1}});
            return false;
        });

        /* 表格工具条点击事件 */
        table.on('tool(regionTable)', function (obj) {
            if (obj.event === 'edit') { // 修改
                showEditModel(obj.data);
            } else if (obj.event === 'del') { // 删除
                doDel(obj);
            }
        });

        /* 表格头工具栏点击事件 */
        table.on('toolbar(regionTable)', function (obj) {
            if (obj.event === 'add') { // 添加
                showEditModel();
            } else if (obj.event === 'del') { // 删除
                var checkRows = table.checkStatus('regionTable');
                if (checkRows.data.length === 0) {
                    layer.msg('请选择要删除的数据', {icon: 2});
                    return;
                }
                var ids = checkRows.data.map(function (d) {
                    return d.id;
                });
                doDel({ids: ids});
            }
        });

        /* 显示表单弹窗 */
        function showEditModel(mData) {
            console.log(mData);
            admin.open({
                type: 1,
                title: (mData ? '修改' : '添加') + '信息',
                content: $('#regionEditDialog').html(),
                success: function (layero, dIndex) {
                    // 回显表单数据
                    form.val('regionEditForm', mData);
                    // 表单提交事件
                    form.on('submit(regionEditSubmit)', function (data) {
                        var loadIndex = layer.load(2);

                        $.post(mData ? '/api/admin/Region/EditRegionInfo' : '/api/admin/Region/AddRegionInfo', data.field, function (res) {
                                layer.close(loadIndex);
                                if (1 == res.code) {
                                    layer.close(dIndex);
                                    layer.msg(res.message, { icon: 1 });
                                    insTb.reload({ page: { curr: 1 } });
                                } else {
                                    layer.msg(res.message, { icon: 2 });
                                }
                         }, 'json');

                        return false;
                    });
                }
            });
        }

        /* 删除 */
        function doDel(obj) {
            layer.confirm('确定要删除选中数据吗？', {
                skin: 'layui-layer-admin',
                shade: .1
            }, function (i) {
                layer.close(i);
                var loadIndex = layer.load(2);
                    $.post('/api/admin/Region/DeleteRegionInfo', {
                        ids: obj.data ? obj.data.id : obj.ids.join(',')
                    }, function (res) {
                        layer.close(loadIndex);
                        if (res.code == 1) {
                            layer.msg(res.message, { icon: 1 });
                            insTb.reload({ page: { curr: 1 } });
                        } else {
                            layer.msg(res.message, { icon: 2 });
                        }
                    }, 'json');
            });
        }

    });
</script>